package cn.com.xf.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.xf.dao.TblRegExpressDAO;
import cn.com.xf.dao.TblRegVisitorDAO;
import cn.com.xf.domain.TblRegExpress;
import cn.com.xf.domain.TblRegVisitor;
import cn.com.xf.service.VisitorService;
import cn.com.xf.utils.CodeStr;
import cn.com.xf.web.form.ExpressForm;
import cn.com.xf.web.form.VisitorForm;

@Service
public class VisitorServiceImpl implements VisitorService{
	
	@Autowired
	private TblRegExpressDAO regExpressDao;
	
	@Autowired
	private TblRegVisitorDAO regVisitorDao;

	@Override
	public TblRegVisitor visitorById(String vid) {		
		return regVisitorDao.selectById(vid);
	}

	@Override
	public int appointAudit(VisitorForm vForm) {
		int result=0;
		String id = vForm.getVid();
		result = regVisitorDao.vauditById(id);		
		return result;
	}

	

	public int visitorOut(String id) {
		TblRegVisitor trv0 =regVisitorDao.selectById(id);
		if(trv0.getOuttime() == null){
			TblRegVisitor trv = new TblRegVisitor();
			trv.setOuttimeStr(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
			trv.setVid(id);
			return regVisitorDao.outById(trv);
		}
		return 0;
		
		
	}

	@Override
	public int visitorNew(VisitorForm vForm) {
		TblRegVisitor trv = new TblRegVisitor();
		trv.setVname(vForm.getVname());
		trv.setCerttype(vForm.getCerttype());
		trv.setCert(vForm.getCert());
		trv.setUnits(vForm.getUnits());
		trv.setPhone(vForm.getPhone());
		trv.setDepa(vForm.getDepa());
		trv.setByvisitor(vForm.getByvisitor());
		trv.setAim(vForm.getAim());
		trv.setVisittimeStr(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		trv.setRemark(vForm.getRemark());
		trv.setUsername(vForm.getUsername());
		trv.setUserid(vForm.getUserid());
		trv.setPeoplenum(vForm.getPeoplenum());
		trv.setPhotoname(vForm.getPhotoname());
		trv.setStatus("1");		
		int result = 0;
		try {
			regVisitorDao.insertVisit(trv);;
			result = 1;
		} catch (Exception e) {
			result = 0;
		}		
		return result;
	}

	@Override
	public int visitorModify(VisitorForm vForm) {
		int result = 0;
		TblRegVisitor trv = new TblRegVisitor();
		trv.setVid(vForm.getVid());
		trv.setVname(vForm.getVname());
		trv.setCerttype(vForm.getCerttype());
		trv.setCert(vForm.getCert());
		trv.setUnits(vForm.getUnits());
		trv.setPhone(vForm.getPhone());
		trv.setDepa(vForm.getDepa());
		trv.setByvisitor(vForm.getByvisitor());
		trv.setAim(vForm.getAim());
		trv.setRemark(vForm.getRemark());
		trv.setPeoplenum(vForm.getPeoplenum());
		trv.setPhotoname(vForm.getPhotoname());
		if(vForm.getVaudit() != null && vForm.getVaudit().equals("1")){
			trv.setVisittimeStr(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
			trv.setStatus("1");
			trv.setUsername(vForm.getUsername());
			trv.setUserid(vForm.getUserid());
		}		
		try {
			regVisitorDao.updateVisit(trv);
			result = 1;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			result = 0;
		}		
		return result;
	}
	
	
	@Override
	public List<TblRegVisitor> visitorReg(VisitorForm form) {
		TblRegVisitor trv = new TblRegVisitor();
		trv.setVname(form.getVname());
		trv.setStaffid(form.getStaffid());
		try {
			return regVisitorDao.selectReg(trv);
		} catch (Exception e) {
			return null;
		}
		 
	}
	
	public Map<String,Integer> visitorIntraday(){
		List<TblRegVisitor> list = regVisitorDao.selectIntraday();
		Integer intradayCount=list.size();
		Integer outCount=0;		
		for(TblRegVisitor trv:list){
			if(trv.getStatus().equals("2")){
				outCount++;
			}
		}
		Map<String,Integer> map = new HashMap<String, Integer>();
		map.put("intradayCount", intradayCount);
		map.put("notOutCount", intradayCount-outCount);
		return map;		
	}

	@Override
	public List<TblRegVisitor> visitorList(int begin, int pageSize, String orderByField, String orderByMode,
			String username, String vname, String vid, String visittimeStart, String visittimeEnd) {
		return regVisitorDao.selectRegVisitor(begin, pageSize, orderByField, orderByMode, username, vname, vid, visittimeStart, visittimeEnd);
	}

	@Override
	public int visitorCount(String username, String vname, String vid, String visittimeStart, String visittimeEnd) {
		return regVisitorDao.selectCountVisit(username, vname, vid, visittimeStart, visittimeEnd);
	}
	
	
	
	
	/*快递*/
	@Override
	public TblRegExpress expressById(Integer id) {
		return regExpressDao.selectById(id);
	}

	

	@Override
	public int expressDel(ExpressForm form) {
		int result=0;
		int id = form.getExid();
		String userid = form.getUserid();
		TblRegExpress tre =regExpressDao.selectById(id);
		if(tre.getUserid().equals(userid)){
			regExpressDao.deleteById(id);
			result = 1;						
		}else{
			result = 0;
		}		
		return result;
	}

	@Override
	public int expressRepeatCount(String exnum) {
		return regExpressDao.select30DayCount(exnum);
	}	
	
	@Override
	public int expressNew(ExpressForm eForm) {
		TblRegExpress tre = new TblRegExpress();
		tre.setExcompany(eForm.getExcompany());
		tre.setExnum(eForm.getExnum());
		tre.setReceiver(eForm.getReceiver());
		tre.setAddtimeStr(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		if(eForm.getExaudit() == null){
			tre.setExaudit("0");
		}else{
			tre.setExaudit(eForm.getExaudit());
		}		
		tre.setUsername(eForm.getUsername());
		tre.setUserid(eForm.getUserid());
		int result = 0;
		try {
			regExpressDao.insert(tre);
			result = 1;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			result = 0;
		}
		return result;
	}

	@Override
	public int expressModify(ExpressForm eForm) {
		int result = 0;
		TblRegExpress tre = new TblRegExpress();
		tre.setExcompany(eForm.getExcompany());
		tre.setExnum(eForm.getExnum());
		tre.setReceiver(eForm.getReceiver());
		tre.setExid(eForm.getExid());
		try {
			regExpressDao.update(tre);
			result = 1;
		} catch (Exception e) {
			result = 0;
		}		
		return result;
	}

	@Override
	public List<TblRegExpress> expressList(int begin, int pageSize, String orderByField, String orderByMode,
			String username, String exnum,String addtimeStart,String addtimeEnd) {
		return regExpressDao.selectRegExpress(begin, pageSize, orderByField, orderByMode, username, exnum, addtimeStart, addtimeEnd);
	}

	@Override
	public int expressCount(String username, String exnum,String addtimeStart,String addtimeEnd) {
		return regExpressDao.selectCount(username, exnum, addtimeStart, addtimeEnd);
	}

	@Override
	public int appointDel(String vid) {
		int result = 0;
		TblRegVisitor trv = regVisitorDao.selectById(vid);
		if(trv.getStatus().equals("0")){
			result = regVisitorDao.delAppointById(vid);			
		}		
		return result;
	}

	@Override
	public int appointNew(VisitorForm vForm) {
		TblRegVisitor trv = new TblRegVisitor();
		trv.setVname(vForm.getVname());
		trv.setCerttype(vForm.getCerttype());
		trv.setCert(vForm.getCert());
		trv.setUnits(vForm.getUnits());
		trv.setPhone(vForm.getPhone());
		trv.setDepa(vForm.getDepa());
		trv.setByvisitor(vForm.getByvisitor());
		trv.setAim(vForm.getAim());
		trv.setAppointtimeStr(vForm.getAppointtime());
		trv.setRemark(vForm.getRemark());
		trv.setStaffname(vForm.getStaffname());
		trv.setStaffid(vForm.getStaffid());
		trv.setPeoplenum(vForm.getPeoplenum());
		trv.setStatus("0");
		trv.setAppoint("1");
		trv.setVaudit(vForm.getVaudit()==null?"0":vForm.getVaudit());
		int result = 0;
		try {
			regVisitorDao.insertAppoint(trv);
			result = 1;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			result = 0;
		}		
		return result;
	}

	@Override
	public int appointModify(VisitorForm vForm) {
		int result = 0;
		TblRegVisitor trv1 = regVisitorDao.selectById(vForm.getVid());
		if(trv1.getStatus().equals("1") || trv1.getStatus().equals("2")){
			result = 2;
		}
		TblRegVisitor trv = new TblRegVisitor();
		trv.setVid(vForm.getVid());
		trv.setVname(vForm.getVname());
		trv.setCerttype(vForm.getCerttype());
		trv.setCert(vForm.getCert());
		trv.setUnits(vForm.getUnits());
		trv.setPhone(vForm.getPhone());
		trv.setDepa(vForm.getDepa());
		trv.setByvisitor(vForm.getByvisitor());
		trv.setAim(vForm.getAim());
		trv.setRemark(vForm.getRemark());
		trv.setPeoplenum(vForm.getPeoplenum());
		trv.setAppointtimeStr(vForm.getAppointtime());
		try {
			regVisitorDao.updateAppoint(trv);
			result = 1;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			result = 0;
		}		
		return result;
	}

	@Override
	public int appointToVisit(VisitorForm form) {		
		TblRegVisitor trv = new TblRegVisitor();
		trv.setVid(form.getVid());
		trv.setUsername(form.getUsername());
		trv.setUserid(form.getUserid());
		trv.setVisittimeStr(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		return regVisitorDao.appointToVisit(trv);
	}
		

	@Override
	public int appointCount(String staffid, String vname,String status,String vaudit, String appointtimeStart,
			String appointtimeEnd) {
		return regVisitorDao.selectCountAppoint(staffid, vname,status,vaudit, appointtimeStart, appointtimeEnd);
	}

	@Override
	public List<TblRegVisitor> appointList(int begin, int pageSize, String orderByField, String orderByMode,
			String staffid, String vname, String status,String vaudit, String appointtimeStart, String appointtimeEnd) {
		return regVisitorDao.selectAppoint(begin, pageSize, orderByField, orderByMode, staffid, vname, status, vaudit, appointtimeStart, appointtimeEnd);
	}

	
	


	

	
}
